<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="btnCancel">
    <el-form ref="addForm" label-width="120px" :model="addForm" :rules="addRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="addForm.name" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="addForm.code" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="addForm.managerId" style="width: 80%;" size="mini" placeholder="请选择负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model.trim="addForm.introduce" type="textarea" style="width: 80%;" size="mini" :rows="4" placeholder="1-100个字符" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row style="width:100%;" type="flex" justify="end">
        <el-button size="mini" type="text" style="color: #727479;margin-right:20px" @click="btnCancel">取消</el-button>
        <el-button size="mini" type="primary" @click="btnOk">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addDeptAPI, getDepartMentAPI, getDepartMentDetailAPI, getUsernameAPI } from '@/api'

export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      addForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      addRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartMentAPI()
              list.some(item => item.code === value) ? callback(new Error(`当前已有部门${value}编码已存在`)) : callback()
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      },
      managerList: []

    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    btnCancel() {
      this.$refs.addForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async btnOk() {
      await this.$refs.addForm.validate()
      await addDeptAPI({ ...this.addForm, pid: this.currentNodeId })
      this.$message.success('新增部门成功')
      this.btnCancel() // 关闭弹层 重置表单
      // 通知父组件更新
      this.$emit('updateDept')
    },
    async getUserList() {
      this.managerList = await getUsernameAPI()
    },
    async getDepartMentDetail(id) {
      this.addForm = await getDepartMentDetailAPI(id)
    }
  }
}
</script>
<style scoped>
</style>
